package net.caixiaomi.info.ui.withdraw;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;

import net.caixiaomi.info.R;
import net.caixiaomi.info.adapter.WithdrawDetailAdapter;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.BaseActivity;
import net.caixiaomi.info.base.progressbar.CircularProgressBar;
import net.caixiaomi.info.model.BaseCallModel;
import net.caixiaomi.info.model.WithdrawDetailModel;
import net.caixiaomi.info.net.RequestService;
import net.caixiaomi.info.net.ResponseCallback;
import net.caixiaomi.info.net.ResponseError;
import net.caixiaomi.info.net.RetrofitManage;
import net.caixiaomi.info.util.ToastUtil;
import net.caixiaomi.smartrefresh.layout.SmartRefreshLayout;

import butterknife.BindView;
import retrofit2.Call;

/**
 * 提现详情
 */
public class WithdrawDetailActivity extends BaseActivity {

    @BindView(R.id.refresh)
    SmartRefreshLayout mRefresh;
    @BindView(android.R.id.list)
    RecyclerView mListView;
    @BindView(R.id.progress)
    CircularProgressBar mProgress;

    private TextView mHeaderView;
    private TextView mBankName;

    private WithdrawDetailAdapter mAdapter;
    private String mWithdrawSn;

    @Override
    protected int layout() {
        return R.layout.act_default_list;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mWithdrawSn = getIntent().getStringExtra("data");
        initView();
        loadData();
    }

    private void initView() {
        mRefresh.setEnableRefresh(false);
        mHeaderView = new TextView(this);
        mHeaderView.setBackgroundColor(ContextCompat.getColor(CommonApp.getInstance(), R.color.white));
        mHeaderView.setGravity(Gravity.CENTER);
        mHeaderView.setTextColor(ContextCompat.getColor(CommonApp.getInstance(), R.color.secondary_text));
        mHeaderView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.font_12));
        mHeaderView.setPadding(0, (int) getResources().getDimension(R.dimen.padding_30), 0, (int) getResources().getDimension(R.dimen.padding_30));

        View footerView = LayoutInflater.from(this).inflate(R.layout.footer_withdraw_detail, null);
        mBankName = (TextView) footerView.findViewById(R.id.name);

        mAdapter = new WithdrawDetailAdapter(R.layout.item_withdraw_detail);
        mAdapter.addHeaderView(mHeaderView);
        mAdapter.addFooterView(footerView);
        mListView.setLayoutManager(new LinearLayoutManager(this));
        mListView.setAdapter(mAdapter);
    }

    private void loadData() {
        JSONObject params = new JSONObject();
        params.put("withdrawSn", mWithdrawSn);
        RequestService requestService = RetrofitManage.getInstance().getRequestService();
        Call<BaseCallModel<WithdrawDetailModel>> withdrawInfo = requestService.withdrawInfo(params);
        withdrawInfo.enqueue(new ResponseCallback<BaseCallModel<WithdrawDetailModel>>(this) {
            @Override
            public void onSuccess(BaseCallModel<WithdrawDetailModel> model) {
                mProgress.setVisibility(View.GONE);
                WithdrawDetailModel data = model.data;
                bindData(data);
            }

            @Override
            public void onFail(ResponseError error) {
                mProgress.setVisibility(View.GONE);
                if (error.getCode() >= 300000 && error.getCode() <= 310000) {
                    ToastUtil.s(error.getMessage());
                }
            }

            @Override
            public void onError() {
                mProgress.setVisibility(View.GONE);
            }
        });

    }

    private void bindData(WithdrawDetailModel data) {
        try {
            StringBuilder stringBuilder = new StringBuilder();
            String price = stringBuilder.append(getString(R.string.C_MONEY_CHAT)).append(data.getAmount()).toString();
            stringBuilder.append("\n").append(data.getStatus());
            SpannableString spannableString = new SpannableString(stringBuilder);
            spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.orange_primary)), 0, price.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            spannableString.setSpan(new RelativeSizeSpan(2f), 0, 1, SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            spannableString.setSpan(new RelativeSizeSpan(3f), 1, price.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            mHeaderView.setText(spannableString);

            mBankName.setText(data.getCard());

            mAdapter.setNewData(data.getUserWithdrawLogs());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
